/*
 * For legal stuff see the file COPYRIGHT
 */
#import "Preferences.h"

@implementation Preferences

/* Attribute defaults */
#define	DFLT_INVOICE_NUM	"0"
#define DFLT_INVOICE_DIR	"/tmp"
#define DFLT_HIDE_ON_AUTO	"NO"
#define DFLT_SHOW_END_TIMES	"NO"
#define DFLT_CONSULTANT_INFO	""

/* Positions in the Form */
#define	MY_NAME_POS		0
#define	MY_COMPANY_POS		1
#define	MY_STREET_POS		2
#define	MY_CITY_POS		3
#define	MY_STATE_POS		4
#define	MY_ZIP_POS		5
#define MY_PHONE_POS		6
#define	MY_FAX_POS		7
#define	MY_EMAIL_POS		8

static BOOL
yes( const char *attr )
{
  const char *value = GET_DEFAULT(attr);
  return ( value && strcmp( value, "YES") == 0 ? YES : NO );
}

+ initialize
{
  static NXDefaultsVector dflts = {
    { INVOICE_NUM,	DFLT_INVOICE_NUM },
    { INVOICE_DIR,	DFLT_INVOICE_DIR },
    { HIDE_ON_AUTO,	DFLT_HIDE_ON_AUTO },
    { SHOW_END_TIMES,	DFLT_SHOW_END_TIMES },
    { MY_NAME,		"" },
    { MY_COMPANY,	"" },
    { MY_STREET,	"" },
    { MY_CITY,		"" },
    { MY_STATE,		"" },
    { MY_ZIP,		"" },
    { MY_PHONE,		"" },
    { MY_FAX,		"" },
    { MY_EMAIL,		"" },
    { NULL }
  };

  NXRegisterDefaults( OWNER, dflts ) ;
  return self ;
}

/*
 * Allow only one preferences object for this app
 */
+ new
{
  static id prefs;

  if ( ! prefs ) {
    prefs = [[Preferences alloc] init];
    [NXApp loadNibSection:"Preferences.nib" owner:prefs withNames:NO];
    [prefs revert:nil];
  }
    
  return prefs;
}

/*
 * Called by the controller to see if we should hide ourselves.
 * Return YES if we were autolaunched and the user said to hide.
 */
- (BOOL)hideOnAutoLaunch
{
  return( yes( "NXAutoLaunch" ) && yes(HIDE_ON_AUTO) );
}

- (BOOL)showEndTimes
{
  return( yes(SHOW_END_TIMES) );
}

- display
{
  [invoiceNumberText selectText:nil];
  [window makeKeyAndOrderFront:self];
  return self;
}

- chooseDir:sender
{
  OpenPanel *panel = [OpenPanel new];
  int result;

  [panel chooseDirectories:YES];
  result = [panel runModalForDirectory:NXHomeDirectory() file:""];
  if ( result != NX_RUNABORTED )
    [directoryText setStringValue:[panel filename]];
  return self;
}

- ok:sender
{
  PUT_DEFAULT( INVOICE_NUM,   [invoiceNumberText stringValue] ) ;
  PUT_DEFAULT( INVOICE_DIR,   [directoryText stringValue] ) ;
  PUT_DEFAULT( HIDE_ON_AUTO,  [hideButton state]     ? "YES" : "NO" ) ;
  PUT_DEFAULT( SHOW_END_TIMES,[endTimesButton state] ? "YES" : "NO" ) ;

  PUT_DEFAULT( MY_NAME,    [form stringValueAt:MY_NAME_POS] );
  PUT_DEFAULT( MY_COMPANY, [form stringValueAt:MY_COMPANY_POS] );
  PUT_DEFAULT( MY_STREET,  [form stringValueAt:MY_STREET_POS] );
  PUT_DEFAULT( MY_CITY,    [form stringValueAt:MY_CITY_POS] );
  PUT_DEFAULT( MY_STATE,   [form stringValueAt:MY_STATE_POS] );
  PUT_DEFAULT( MY_PHONE,   [form stringValueAt:MY_PHONE_POS] );
  PUT_DEFAULT( MY_ZIP,     [form stringValueAt:MY_ZIP_POS] );
  PUT_DEFAULT( MY_FAX,     [form stringValueAt:MY_FAX_POS] );
  PUT_DEFAULT( MY_EMAIL,   [form stringValueAt:MY_EMAIL_POS] );

  [window close];
  return self;
}

- revert:sender
{
  NXUpdateDefaults() ;		/* reload the table from the database */

  [invoiceNumberText setStringValue:GET_DEFAULT(INVOICE_NUM)];
  [directoryText     setStringValue:GET_DEFAULT(INVOICE_DIR)];
  [hideButton        setState:yes(HIDE_ON_AUTO)];
  [endTimesButton    setState:yes(SHOW_END_TIMES)];

  [form setStringValue:GET_DEFAULT(MY_NAME)    at:MY_NAME_POS];
  [form setStringValue:GET_DEFAULT(MY_COMPANY) at:MY_COMPANY_POS];
  [form setStringValue:GET_DEFAULT(MY_STREET)  at:MY_STREET_POS];
  [form setStringValue:GET_DEFAULT(MY_CITY)    at:MY_CITY_POS];
  [form setStringValue:GET_DEFAULT(MY_STATE)   at:MY_STATE_POS];
  [form setStringValue:GET_DEFAULT(MY_ZIP)     at:MY_ZIP_POS];
  [form setStringValue:GET_DEFAULT(MY_PHONE)   at:MY_PHONE_POS];
  [form setStringValue:GET_DEFAULT(MY_FAX)     at:MY_FAX_POS];
  [form setStringValue:GET_DEFAULT(MY_EMAIL)   at:MY_EMAIL_POS];

  return self;
}

- reset:sender
{
  int pos;

  [invoiceNumberText setStringValue:DFLT_INVOICE_NUM];
  [directoryText     setStringValue:DFLT_INVOICE_DIR];
  [hideButton        setState:yes(DFLT_HIDE_ON_AUTO)];
  [endTimesButton    setState:yes(DFLT_SHOW_END_TIMES)];

  for ( pos = 0; pos <= MY_EMAIL_POS; pos++ )
    [form setStringValue:"" at:pos];    

  return self;
}

- (const char *)myName
{
  return GET_DEFAULT(MY_NAME);
}

- (const char *)myCompany
{
  return GET_DEFAULT(MY_COMPANY);
}

- (const char *)myStreet
{
  return GET_DEFAULT(MY_STREET);
}

- (const char *)myCity
{
  return GET_DEFAULT(MY_CITY);
}

- (const char *)myState
{
  return GET_DEFAULT(MY_STATE);
}

- (const char *)myZip
{
  return GET_DEFAULT(MY_ZIP);
}

- (const char *)myPhone
{
  return GET_DEFAULT(MY_PHONE);
}

- (const char *)myFax
{
  return GET_DEFAULT(MY_FAX);
}

- (const char *)myEmail
{
  return GET_DEFAULT(MY_EMAIL);
}

@end
